package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.admin.service.MenuService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Menu;
import com.his.dao.domain.vo.MenuListVo;
import com.his.dao.domain.vo.MenuVo;
import com.his.dao.domain.vo.SelectAllVo;
import com.his.dao.mapper.MenuMapper;
import com.his.util.constant.RedisConstant;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import com.his.util.util.StringUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private MenuMapper menuMapper;

    @Override
    public RestResult load(String authorization) {
        int userId = StringUtil.getId(RedisConstant.USER,
                (String) redisTemplate.opsForValue().get(RedisConstant.getUserAndAuthorization(authorization)));

        List<Menu> menuList = menuMapper.selectAllByRoleId(userId);
        List<MenuVo> menuVoList = new ArrayList<>();
        Map<Integer, List<MenuVo>> menuMap = new HashMap<>();

        for (Menu menu : menuList) {
            MenuVo menuVo = MenuVo
                    .builder()
                    .name(menu.getName())
                    .path(menu.getPath())
                    .component(menu.getComponent())
                    .icon(menu.getIcon())
                    .title(menu.getTitle())
                    .children(new ArrayList<>())
                    .build();
            if (menu.getParentId() == 0) {
                menuVoList.add(menuVo);
            } else {
                menuMap.get(menu.getParentId()).add(menuVo);
            }

            menuMap.put(menu.getMenuId(), menuVo.getChildren());
        }

        return new RestResultBuilder().success(menuVoList);
    }

    @Override
    public RestResult all(ListDto listDto) {
        Menu menu = MapUtil.conversionMapToObj((Map) listDto.getObj(), Menu.class);
        IPage iPage = new Page(listDto.getPage(), listDto.getPageSize());
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<Menu>()
                .eq("parent_id", menu.getMenuId());

        IPage<Menu> menuIPage = menuMapper.selectPage(iPage, menuQueryWrapper);

        List<Object> menuListVos = new ArrayList<>();

        for (Menu menu1 : menuIPage.getRecords()) {
            menuListVos.add(MenuListVo
                    .builder()
                    .menuId(menu1.getMenuId())
                    .path(menu1.getPath())
                    .name(menu1.getName())
                    .title(menu1.getTitle())
                    .icon(menu1.getIcon())
                    .component(menu1.getComponent())
                    .isOpen(menu1.getIsOpen())
                    .children(new ArrayList<>())
                    .hasChildren(true)
                    .build());
        }

        SelectAllVo selectAllVo = SelectAllVo
                .builder()
                .list(menuListVos)
                .total(iPage.getTotal())
                .build();

        return new RestResultBuilder().success(selectAllVo);
    }

    @Override
    public RestResult selectAllMenuId(ListDto listDto) {
        String name = (String) listDto.getObj();

        IPage iPage = new Page(listDto.getPage(), listDto.getPageSize());
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<Menu>()
                .select("menu_id", "name")
                .like(name != null, "name", name);

        IPage<Menu> menuIPage = menuMapper.selectPage(iPage, menuQueryWrapper);

        return new RestResultBuilder().success(menuIPage.getRecords());
    }

}
